import { defineStore } from 'pinia';
import { store } from '/@/store';

interface GraphState {
  currentPatentId: string;
  currentPatentName: string;
  relationType: string;
  autoOpenDetail: boolean;  // 标记是否需要自动打开详情
}

export const useGraphStore = defineStore({
  id: 'app-graph',
  state: (): GraphState => ({
    currentPatentId: '',
    currentPatentName: '',
    relationType: 'institution',
    autoOpenDetail: false,
  }),
  getters: {
    getCurrentPatentId(): string {
      return this.currentPatentId;
    },
    getCurrentPatentName(): string {
      return this.currentPatentName;
    },
    getRelationType(): string {
      return this.relationType;
    },
    getAutoOpenDetail(): boolean {
      return this.autoOpenDetail;
    },
  },
  actions: {
    /**
     * 设置当前专利
     */
    setCurrentPatent(patentId: string, patentName?: string) {
      this.currentPatentId = patentId;
      this.currentPatentName = patentName || '';
      console.log('[GraphStore] 设置当前专利:', { patentId, patentName });
    },
    
    /**
     * 设置关联类型
     */
    setRelationType(type: string) {
      this.relationType = type;
      console.log('[GraphStore] 设置关联类型:', type);
    },
    
    /**
     * 清空当前专利
     */
    clearCurrentPatent() {
      this.currentPatentId = '';
      this.currentPatentName = '';
      console.log('[GraphStore] 清空当前专利');
    },
    
    /**
     * 设置是否自动打开详情
     */
    setAutoOpenDetail(auto: boolean) {
      this.autoOpenDetail = auto;
      console.log('[GraphStore] 设置自动打开详情:', auto);
    },
  },
});

// Need to be used outside the setup
export function useGraphStoreWithOut() {
  return useGraphStore(store);
}

